package com.tian.view.anim.attr_anim

import android.animation.AnimatorInflater
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.tian.view.R
import com.tian.view.databinding.ControlViewListBinding

/**
 * @author tiansongtao, Email:tiansongtao@up366.com
 * @date 2022/12/14 17:57
 *
 * AnimatorSet 动画合集，很好理解，就是多个动画的执行。
 * 常用函数：
 *  1. playSequentially：表示合集动画顺序执行，一个执行完执行下一个
 *  2. playTogether：表示合集动画全部一起执行
 *  3. AnimatorSet.Builder
 *      AnimatorSet.play函数构建一个Builder，表示播放这个动画
 *      with，before, after都是以play动画为基准，
 *          with:表示与play动画一起执行 eg: alpha与rotation会一起执行。
 *          before:表示play动画会在这个动画之前执行。eg: alpha会在translation之前执行
 *          after:表示play动画会在这个动画之后执行。eg: alpha会在scale之后执行
 */
class AnimatorSetFragment : Fragment() {

    private lateinit var b: ControlViewListBinding


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        b = ControlViewListBinding.inflate(inflater)
        return b.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        (requireActivity() as AppCompatActivity).supportActionBar?.title = "ObjectAnimator"
        initPlaySequentially()
        initPlayTogether()
        initAnimatorTranslation()
        initXmlAnimator()
    }

    private fun initPlaySequentially() {
        b.mbt1.text = "AnimatorSet顺序执行-playSequentially"
        b.mbt1.visibility = View.VISIBLE
        b.mbt1.setOnClickListener {
            val alpha = ObjectAnimator.ofFloat(b.mbtShow, "alpha", 1f, 0f, 1f)
            val rotation = ObjectAnimator.ofFloat(b.mbtShow, "rotationX", 1f, 180f, 0f)
            val translation = ObjectAnimator.ofFloat(b.mbtShow, "translationX", 0f, 400f, 0f)
            val set = AnimatorSet()
            // playSequentially：表示合集动画顺序执行，一个执行完执行下一个
            set.playSequentially(alpha, rotation, translation)
            set.duration = 4000
            set.start()
        }
    }

    private fun initPlayTogether() {
        b.mbt2.text = "AnimatorSet一起执行-playTogether"
        b.mbt2.visibility = View.VISIBLE
        b.mbt2.setOnClickListener {
            val alpha = ObjectAnimator.ofFloat(b.mbtShow, "alpha", 1f, 0f, 1f)
            val rotation = ObjectAnimator.ofFloat(b.mbtShow, "rotationX", 1f, 180f, 0f)
            val translation = ObjectAnimator.ofFloat(b.mbtShow, "translationX", 0f, 400f, 0f)
            val set = AnimatorSet()
            // playTogether：表示合集动画全部一起执行
            set.playTogether(alpha, rotation, translation)
            set.duration = 4000
            set.start()
        }
    }

    private fun initAnimatorTranslation() {
        b.mbt3.text = "AnimatorSet Builder"
        b.mbt3.visibility = View.VISIBLE
        b.mbt3.setOnClickListener {
            val alpha = ObjectAnimator.ofFloat(b.mbtShow, "alpha", 1f, 0f, 1f)
            val rotation = ObjectAnimator.ofFloat(b.mbtShow, "rotationX", 1f, 180f, 0f)
            val translation = ObjectAnimator.ofFloat(b.mbtShow, "translationX", 0f, 400f, 0f)
            val scale = ObjectAnimator.ofFloat(b.mbtShow, "scaleX", 1f, 4f, 1f)
            val set = AnimatorSet()
            /**
             * AnimatorSet.play函数构建一个Builder，表示播放这个动画
             * with，before, after都是以play动画为基准，
             *  with:表示与play动画一起执行 eg: alpha与rotation会一起执行。
             *  before:表示play动画会在这个动画之前执行。eg: alpha会在translation之前执行
             *  after:表示play动画会在这个动画之后执行。eg: alpha会在scale之后执行
             * with,before,after调用多次，同一函数动画会一起执行
             *  eg: set.play(alpha).before(translation).before(scale)
             *      这里 translation与scale会一起执行
             */
            set.play(alpha).with(rotation).before(translation).after(scale)
            set.duration = 4000
            set.start()
        }
    }

    private fun initXmlAnimator() {
        b.mbt7.text = "xml ObjectAnimator"
        b.mbt7.visibility = View.VISIBLE
        b.mbt7.setOnClickListener {
            val anim =
                AnimatorInflater.loadAnimator(requireContext(), R.animator.first_object_animator_test) as AnimatorSet
            anim.setTarget(b.mbtShow)
            anim.start()
        }
    }

}